package org.sharelife.service.impl;

import java.util.Date;
import java.util.List;

import org.sharelife.dao.IMessageDAO;
import org.sharelife.entity.Message;
import org.sharelife.service.MessageService;
import org.sharelife.util.PageBean;

public class MessageServiceImpl implements MessageService {

	private IMessageDAO iMessageDAO;

	// 添加评论
	@Override
	public void add(Message message) throws Exception {
		try {
			message.setPublishTime(new Date());
			iMessageDAO.add(message);
		} catch (Exception e) {
			throw e;
		}
	}

	// 根据id删除评论
	@Override
	public void deleteById(int id) throws Exception {
		try {
			iMessageDAO.deleteById(id);
		} catch (Exception e) {
			throw e;
		}
	}

	// 查询所有评论
	@Override
	public List<Message> findAll() throws Exception {
		try {
			return iMessageDAO.findAll();
		} catch (Exception e) {
			throw e;
		}
	}

	// 根据发布时间查询前5条记录
	@Override
	public List<Message> findByDate() throws Exception {

		try {
			return iMessageDAO.findByDate();
		} catch (Exception e) {
			throw e;
		}
	}

	// 根据ID查询留言对象
	@Override
	public Message findById(int id) throws Exception {
		try {
			return iMessageDAO.findById(id);
		} catch (Exception e) {
			throw e;
		}
	}

	// 根据用户ID分页查询
	@Override
	public List<Message> findByUserId(int id) throws Exception {
		try {
			return iMessageDAO.findByUserId(id);
		} catch (Exception e) {
			throw e;
		}
	}

	public IMessageDAO getiMessageDAO() {
		return iMessageDAO;
	}

	// 修改评论信息
	@Override
	public void modify(Message message) throws Exception {
		try {
			iMessageDAO.modify(message);
		} catch (Exception e) {
			throw e;
		}
	}

	// 分页查询
	@Override
	public PageBean queryForPage(int pageSize, int page) throws Exception {
		try {
			return iMessageDAO.queryForPage(pageSize, page);
		} catch (Exception e) {
			throw e;
		}
	}

	// 根据关键字分页查询评论
	@Override
	public PageBean queryForPageByKeyWords(String keyWord, int pageSize,
			int page) throws Exception {
		try {
			return iMessageDAO.queryForPageByKeyWords(keyWord, pageSize, page);
		} catch (Exception e) {
			throw e;
		}
	}

	// 根据图片id分页查询评论
	@Override
	public PageBean queryForPageByPictureId(int id, int pageSize, int page)
			throws Exception {
		try {
			return iMessageDAO.queryForPageByPictureId(id, pageSize, page);
		} catch (Exception e) {
			throw e;
		}
	}

	// 根据用户ID查询所有评论
	@Override
	public PageBean queryForPageByUserId(int id, int pageSize, int page)
			throws Exception {
		try {
			return iMessageDAO.queryForPageByUserId(id, pageSize, page);
		} catch (Exception e) {
			throw e;
		}
	}

	public void setiMessageDAO(IMessageDAO iMessageDAO) {
		this.iMessageDAO = iMessageDAO;
	}

}
